package cn.wolfcode.luowowo.website.controller;


import cn.wolfcode.luowowo.api.cache.domain.TravelStatisVO;
import cn.wolfcode.luowowo.api.cache.prefix.StrategyStatisKeyPrefix;
import cn.wolfcode.luowowo.api.cache.prefix.TravelStatisKeyPrefix;
import cn.wolfcode.luowowo.api.cache.prefix.UserInfoKeyPrefix;
import cn.wolfcode.luowowo.api.cache.service.ITravelStatisRedisService;
import cn.wolfcode.luowowo.api.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import cn.wolfcode.luowowo.website.util.GetEndDayTime;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("mine")
public class UserController {

    @Reference
    private IUserInfoService userInfoService;
    @Reference
    private ITravelService travelService;

    @Reference
    private IUserInfoRedisService userInfoRedisService;

    @Reference
    private ITravelStatisRedisService travelStatisRedisService;

    @Reference
    private IStrategyDetailService strategyDetailService;


    @RequestMapping("home")
    public String homepage (Model model , @UserParam UserInfo userInfo){

        //我的窝游记
        List<Travel> list = travelService.selectByAuthorId(userInfo.getId());
        for (Travel t :list) {
            //从redis中获取值,共享到页面
            TravelStatisVO statisVo = travelStatisRedisService.getStatisVo(t.getId());
            t.setReplynum(statisVo.getReplynum());
            t.setViewnum(statisVo.getViewnum());
            t.setThumbsupnum(statisVo.getThumbsupnum());

        }

        model.addAttribute("travels",list);
        int size = list.size();
        //查询游记个数
        model.addAttribute("count",size);
        //关注对象的添加
       /* UserInfo u2 = userInfoService.get(7L);
        userInfoRedisService.addFollow(UserInfoKeyPrefix.FOLLOW_UID,userInfo.getId(),u2);*/

        //查看关注用户
        List<UserInfo> userInfos = userInfoRedisService.getUserInfoByUserId(UserInfoKeyPrefix.FOLLOW_UID,userInfo.getId());
        model.addAttribute("userInfos",userInfos);
        int followCount = userInfos.size();
        model.addAttribute("followCount",followCount);

        //粉丝对象的添加
       // UserInfo u2 = userInfoService.get(7L);
        //userInfoRedisService.addFollow(UserInfoKeyPrefix.FANS_STAFF_ID,userInfo.getId(),u2);

        //查询我的粉丝
        List<UserInfo> fauses = userInfoRedisService.getUserInfoByUserId(UserInfoKeyPrefix.FANS_STAFF_ID,userInfo.getId());
        model.addAttribute("fauses",fauses);
        int fausCount = fauses.size();
        model.addAttribute("fausCount",fausCount);


        return "myzoe/homepage";
    }



    //我的游记
    @RequestMapping("mytravelnotes")
    public String mytravelnotes (Model model , @UserParam UserInfo userInfo){
        //查看我的所有游记
        List<Travel> list = travelService.selectByAuthorId(userInfo.getId());

        int favornum = 0;
        int replynum = 0;
        int viewnum = 0 ;
        for (Travel t :list) {
            //从redis中获取值,共享到页面
            TravelStatisVO statisVo = travelStatisRedisService.getStatisVo(t.getId());
            favornum+=statisVo.getFavornum();
            replynum+=statisVo.getReplynum();
            viewnum +=statisVo.getViewnum();

            t.setReplynum(statisVo.getReplynum());
            t.setViewnum(statisVo.getViewnum());
            t.setThumbsupnum(statisVo.getThumbsupnum());
        }
        model.addAttribute("favornum",favornum);
        model.addAttribute("replynum",replynum);
        model.addAttribute("viewnum",viewnum);
        model.addAttribute("travels",list);
        //查询游记个数
        int size = list.size();
        model.addAttribute("count",size);
        return "myzoe/mytravelnotes";
    }


    //顶
    @RequestMapping("/Thumbup")
    @ResponseBody
    public Object strategyThumbup(Long sid, @UserParam UserInfo userInfo){
        System.out.println(sid);
        System.out.println(sid);
        System.out.println(sid);
        System.out.println(sid);

        if(userInfo  == null){
            return new AjaxResult("请先登录",AjaxResult.CODE_TYPE_UNLOGIN);
        }
        Long time = GetEndDayTime.getEndTime();//获取截止到23:59:59秒还有多少时间(秒)
        TravelStatisKeyPrefix keyPrefix = new TravelStatisKeyPrefix("travel_statis_thumbsup", time);

        boolean ret =  travelStatisRedisService.thumbupTime(keyPrefix,sid,userInfo.getId());

        return new AjaxResult(ret,"").addData(travelStatisRedisService.getStatisVo(sid));


    }


    //收藏
    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long sid,@UserParam UserInfo userInfo){
        if(userInfo  == null){
            return new AjaxResult("请先登录",AjaxResult.CODE_TYPE_UNLOGIN);
        }
        //站在用户的角度
        boolean ret =  travelStatisRedisService.Userfavor(TravelStatisKeyPrefix.USER_TRAVEL_STATIS_FAVOR,sid,userInfo.getId());
        return new AjaxResult(ret,"").addData(travelStatisRedisService.getStatisVo(sid));
    }






    @RequestMapping("review")
    public String review (){

        return "myzoe/review";
    }


    @RequestMapping("setting")
    public String setting (){

        return "myzoe/setting";
    }


    //我的收藏
    @RequestMapping("travelcollection")
    public String travelcollection (Model model,@UserParam UserInfo userInfo){
        //用户收藏的所有攻略
        List<String>  travelIds= travelStatisRedisService.queryFavorByUserId(TravelStatisKeyPrefix.USER_TRAVEL_STATIS_FAVOR,userInfo.getId());
        List<Travel> travels = new ArrayList<>();
        for (String id  :travelIds) {
            Travel travel = travelService.getById(Long.valueOf(id));
            travels.add(travel);
        }

        //用户的收藏的所有攻略
        List<String>  strategyIds= travelStatisRedisService.queryFavorByUserId(StrategyStatisKeyPrefix.USER_STATIS_FAVOR,userInfo.getId());
        List<StrategyDetail> strategys = new ArrayList<>();
        for (String id :strategyIds){
            StrategyDetail detail = strategyDetailService.getById(Long.valueOf(id));
            strategys.add(detail);
        }
        model.addAttribute("strategys",strategys);
        model.addAttribute("travels",travels);
        return "myzoe/travelcollection";

    }

    @RequestMapping("input")
    public String input(UserInfo userInfo){

        userInfoService.update(userInfo);
        return "myzoe/homepage";
    }


}
